
#define dbPath @"http://dl.dropbox.com/u/21409212/questions.json"
#define soPath @"https://api.stackexchange.com/2.0/questions?order=desc&sort=activity&site=stackoverflow&filter=!9VMK.9UE"
#define dbBadgePath @"http://dl.dropbox.com/u/21409212/%@.json"
#define soBadgePath @"https://api.stackexchange.com/2.0/users/%@?order=desc&sort=reputation&site=stackoverflow"

#import "Downloader.h"
#import "APIDownload.h"
#import "CJSONDeserializer.h"
#import "Question.h"
#import "Answer.h"
#import "QuestionsViewController.h"
#import "AppDelegate.h"

@interface Downloader(){
    NSMutableArray *questionsList;
    id delegate;
}

@end

@implementation Downloader
@synthesize delegate;

-(void)getQuestionsList{
    AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    if(!appDelegate.netStatus){
    [APIDownload downloadWithURL:dbPath delegate:self sel:@selector(didLoadQuestionList:)];
    }
    else{
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSEntityDescription *ent = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:self.managedObjectContext];
        [fetch setEntity:ent];
        NSError *error = nil;
        NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetch error:&error] ;
        [delegate finishedLoading:fetchedItems];
    }
}

+(NSMutableArray*)getAnswersArray:(NSSet*)array{
    NSMutableArray *answersArray = [[NSMutableArray new] autorelease];
    for( NSDictionary* obj in array ) {
        Answer* answer = [Answer answerWithObj:obj];
        [answersArray addObject:answer];
        
    }
    return answersArray;
}

- (void)didLoadQuestionList:(APIDownload*)request {
    questionsList = [[[NSMutableArray alloc] init] autorelease];
    CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
    NSArray *questionArray = [[deserializer deserializeAsDictionary:request.downloadData error:nil]objectForKey:@"items"];
    for( NSDictionary* obj in questionArray ) {
        Question* question = [Question questionWithObj:obj];
        [question loadImage];
        [questionsList addObject:question];
        APIDownload *badgeResponse = [APIDownload downloadWithURL:[NSString stringWithFormat:dbBadgePath,question.userRequestURL] delegate:self sel:@selector(badgeLoaded:)];
        badgeResponse.tag = [questionsList indexOfObject:question];

    }
    
    [delegate finishedLoading: questionsList];
    
}

- (void)badgeLoaded:(APIDownload*)request {
    CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
    NSDictionary *user = [[[deserializer deserializeAsDictionary:request.downloadData error:nil]objectForKey:@"items"] lastObject];
    [Question setBadgesFor:[questionsList objectAtIndex:request.tag] source:user];
    NSIndexPath *index = [NSIndexPath indexPathForRow:request.tag inSection:0];
    [[delegate qTable] reloadRowsAtIndexPaths:[NSArray arrayWithObject:index]
                             withRowAnimation:UITableViewRowAnimationNone];

    
}

@end
